<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="名称">
              <a-input placeholder="请输入名称" v-model="queryParam.productTitle"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="介绍">
              <a-input placeholder="商品介绍" v-model="queryParam.productInfo"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="售价">
                <a-input placeholder="请输入售价" v-model="queryParam.procductPrice"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="进货价">
                <a-input placeholder="请输入进货价" v-model="queryParam.productCostPrice"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="总库存">
                <a-input placeholder="请输入总库存" v-model="queryParam.productAllInventory"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="剩余库存">
                <a-input placeholder="请输入剩余库存" v-model="queryParam.productRemainingInventory"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="成交量">
                <a-input placeholder="请输入成交量" v-model="queryParam.productVolume"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="单个商品利润">
                <a-input placeholder="请输入单个商品利润" v-model="queryParam.productProfile"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="是否上架">
                <a-input placeholder="请输入是否上架" v-model="queryParam.productOnShelf"></a-input>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    <div>
      <a-divider/>
      <span style="margin-right: 30px;">实时盈利合计:{{currentProfile}}元</span>
      <span style="margin-right: 30px;">成本合计:{{cost}}元</span>
      <span>成交量合计:{{soldCount}}件</span>
      <a-divider/>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <!-- 高级查询区域 -->
      <j-super-query :fieldList="fieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>
      <!--      <a-button type="primary" icon="download" @click="handleExportXls('商品表')">导出</a-button>-->
      <!--      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl"-->
      <!--                @change="handleImportExcel">-->
      <!--        <a-button type="primary" icon="import">导入</a-button>-->
      <!--      </a-upload>-->
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
<!--          <a-menu-item key="1" @click="batchDel">-->
<!--            <a-icon type="delete"/>-->
<!--            删除-->
<!--          </a-menu-item>-->
          <a-menu-item key="2" >
            <a-icon type="rise"/>
            实时盈利合计:{{currentProfile}}元
          </a-menu-item>
          <a-menu-item key="3" >
            <a-icon type="rise"/>
            成本合计:{{cost}}元
          </a-menu-item>
          <a-menu-item key="4" >
            <a-icon type="rise"/>
            成交量合计:{{soldCount}}件
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px">统计
          <a-icon type="down"/>
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{
        selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt=""
               style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>
        <!-- 字符串超长截取省略号显示-->
        <span slot="description" slot-scope="text">
          <j-ellipsis :value="text" :length="20"/>
        </span>
        <span slot="parameterRender" slot-scope="text">
          <j-ellipsis :value="text" :length="20"/>
        </span>
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical"/>
          <a @click="turnShelf(record)">{{record.productOnShelf==='0'?'下架':'上架'}}</a>
          <a-divider type="vertical"/>
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down"/></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <pr-product-modal ref="modalForm" @ok="modalFormOk"></pr-product-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import PrProductModal from './modules/PrProductModal'
  import { postAction } from '../../api/manage'
  import JSuperQuery from '@/components/jeecg/JSuperQuery.vue'
  import JEllipsis from '@/components/jeecg/JEllipsis'
  import {initDictOptions, filterDictText} from '@/components/dict/JDictSelectUtil'

  const superQueryFieldList = [
    {
      type: 'string',
      value: 'productTitle',
      text: '商品名称'
    },
    {
      type: 'string',
      value: 'productInfo',
      text: '商品介绍'
    },
    {
      type: 'float',
      value: 'productPrice',
      text: '产品售价'
    },
    {
      type: 'float',
      value: 'productCostPrice',
      text: '进货价'
    },
    {
      type: 'float',
      value: 'productProfile',
      text: '单个产品利润'
    },
    {
      type: 'int',
      value: 'productAllInventory',
      text: '总库存'
    },
    {
      type: 'int',
      value: 'productVolume',
      text: '成交量'
    },
    {
      type: 'int',
      value: 'productRemainingInventory',
      text: '剩余库存'
    },
    {
      type: 'int',
      value: 'productRemainingInventory',
      text: '剩余库存'
    },
    { type: 'select', value: 'productOnShelf', text: '同步状态', dictCode: 'pr_on_shelf' }
  ]

  export default {
    name: 'PrProductList',
    mixins: [JeecgListMixin, mixinDevice],
    components: {
      PrProductModal,
      JSuperQuery,
      JEllipsis
    },
    data() {
      return {
        /* 排序参数 */
        isorter:{
          column: 'productOnShelf',
          order: 'asc',
        },
        currentProfile: 0,
        cost: 0,
        soldCount: 0,
        description: '商品表管理页面',
        fieldList: superQueryFieldList,
        superQueryFlag: false,
        superQueryParams: '',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: 'center',
            customRender: function(t, r, index) {
              return parseInt(index) + 1
            }
          },
          {
            title: '商品图片',
            align: 'center',
            dataIndex: 'productImg',
            scopedSlots: { customRender: 'imgSlot' }
          },
          {
            title: '名称',
            align: 'center',
            dataIndex: 'productTitle',
            scopedSlots: { customRender: 'description' }
          },
          {
            title: '介绍',
            align: 'center',
            dataIndex: 'productInfo',
            scopedSlots: { customRender: 'description' }
          },
          {
            title: '售价(元)',
            align: 'center',
            dataIndex: 'productPrice'
          },
          {
            title: '进货价(元)',
            align: 'center',
            dataIndex: 'productCostPrice'
          },
          {
            title: '单个商品利润(元)',
            align: 'center',
            dataIndex: 'productProfile'
          },
          {
            title: '总库存',
            align: 'center',
            dataIndex: 'productAllInventory'
          },
          {
            title: '成交量',
            align: 'center',
            dataIndex: 'productVolume'
          },
          {
            title: '剩余库存',
            align: 'center',
            dataIndex: 'productRemainingInventory'
          },
          {
            title: '是否上架',
            align: 'center',
            dataIndex: 'productOnShelf',
            customRender: (text, record, index) => {
              return filterDictText(this.shelfDictOptions, text);
            }
          },
          {
            title: '操作',
            dataIndex: 'action',
            align: 'center',
            fixed: 'right',
            width: 147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: '/product/list',
          shelf: '/product/shelf',
          delete: '/product/delete',
          deleteBatch: '/product/deleteBatch',
          exportXlsUrl: '/product/exportXls',
          importExcelUrl: '/product/importExcel'

        },
        dictOptions: {

        },
        shelfDictOptions:[],
      }
    },
    created() {
    },
    computed: {
      importExcelUrl: function() {
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
      }
    },
    methods: {
      initDictConfig() {
        initDictOptions('pr_on_shelf').then((res) => {
          if (res.success) {
            this.shelfDictOptions = res.result;
          }
        });

      },
      refreshData() {
        this.loadData()
        //初始化字典配置 在自己页面定义
        this.initDictConfig()
      },
      onSelectChange(selectedRowKeys, selectionRows) {
        let math = require('mathjs')
        this.selectedRowKeys = selectedRowKeys
        this.selectionRows = selectionRows
        console.log(this.selectionRows)
        //实时盈利合计（选中的多个商品单个利润*成交量之和）
        this.currentProfile = 0
        //成本合计（选中的多个商品的单个进价*商品总库存之和）
        this.cost = 0
        //成交量合计（选中的多个商品的成交量之和）
        this.soldCount = 0
        this.selectionRows.forEach(item => {
          let profile = parseFloat(math.format(math.multiply(math.bignumber(item.productProfile),
            math.bignumber(item.productVolume)), 2))
          this.currentProfile += profile
          let currentCost = parseFloat(math.format(math.multiply(math.bignumber(item.productCostPrice),
            math.bignumber(item.productAllInventory))))
          this.cost += currentCost
          this.soldCount += item.productVolume
        })

      },
      turnShelf(record) {
        if (record.productOnShelf) {
          console.log('turnShelf')
          postAction(this.url.shelf, { id: record.id })
            .then(res => {
              console.log(res)
              if (res.success) {
                this.$message.success(res.message)
                this.refreshData()
              }
            }, err => console.log('上下架操作', err))
        }
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>